package com.sgb.link.view.widget;

import android.text.Layout;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.TextView;

/**
 * Created by WYJ on 2020/9/10.
 */
public class ExpandedTextView {

    private int mMaxLine = 3;
    private StringBuilder mFoldedTxt;//收起的文字
    private StringBuilder mExpandedTxt;//展开的文字

    private boolean isRun;
    private boolean isExpanded;

    public void showText(TextView textView, TextView expandedTextView) {
        expandedTextView.setText("展开");
        ViewTreeObserver vto = textView.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                showFoldedText(textView, expandedTextView);
            }
        });
    }

    public void showTextPick(TextView textView, TextView expandedTextView) {
        expandedTextView.setText("展开");
        ViewTreeObserver vto = textView.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                showFoldedTextPick(textView, expandedTextView);
            }
        });
    }

    public void showTextT(TextView textView, TextView expandedTextView) {
        expandedTextView.setText("展开");
        ViewTreeObserver vto = textView.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                showFoldedTextT(textView, expandedTextView);
            }
        });
    }
    public void showFoldedText(TextView textView, TextView expandedTextView) {
        if (isRun) {
            return;
        }
        isRun = true;
        int lines = textView.getLineCount();
        Layout layout = textView.getLayout();
        mExpandedTxt = new StringBuilder(textView.getText().toString());
        if (lines > mMaxLine) {
            int startLineCount = 0;
            int endLineCount;
            mFoldedTxt = new StringBuilder();
            for (int i = 0; i < mMaxLine; i++) {
                String lineStr = mExpandedTxt.subSequence(layout.getLineStart(i), layout.getLineEnd(i)).toString();
                if (i == 0) {
                    startLineCount = lineStr.length();
                }
                if (i == mMaxLine - 1) {
                    endLineCount = lineStr.length();
                    if (endLineCount * 2 > startLineCount) {
                        String substring = lineStr.substring(0, startLineCount / 2) + "...";
                        mFoldedTxt.append(substring);
                    } else {
                        mFoldedTxt.append(lineStr);
                    }
                } else {
                    mFoldedTxt.append(lineStr);
                }
            }
            textView.setText(mFoldedTxt.toString());
            expandedTextView.setVisibility(View.VISIBLE);
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (isExpanded) {
                        isExpanded = false;
                        textView.setMaxLines(3);
                        textView.setText(mFoldedTxt);
                        expandedTextView.setText("展开");
                        expandedTextView.setVisibility(View.VISIBLE);
                    } else {
                        isExpanded = true;
                        textView.setMaxLines(Integer.MAX_VALUE);
                        textView.setText(mExpandedTxt);
                        expandedTextView.setVisibility(View.GONE);
                    }
                }
            });
        } else {
            textView.setText(mExpandedTxt.toString());
            expandedTextView.setVisibility(View.GONE);
        }
    }
public void showFoldedTextT(TextView textView, TextView expandedTextView) {
    if (isRun) {
        return;
    }
    isRun = true;
    int lines = textView.getLineCount();
    Layout layout = textView.getLayout();
    mExpandedTxt = new StringBuilder(textView.getText().toString());
    if (lines > mMaxLine) {
        int startLineCount = 0;
        int endLineCount;
        mFoldedTxt = new StringBuilder();
        for (int i = 0; i < mMaxLine; i++) {
            String lineStr = mExpandedTxt.subSequence(layout.getLineStart(i), layout.getLineEnd(i)).toString();
            if (i == 0) {
                startLineCount = lineStr.length();
            }
            if (i == mMaxLine - 1) {
                endLineCount = lineStr.length();
                if (endLineCount >= startLineCount * 3 / 4) {
                    String substring = lineStr.substring(0, startLineCount * 3 / 4) + "...";
                    mFoldedTxt.append(substring);
                } else {
                    mFoldedTxt.append(lineStr);
                }
            } else {
                mFoldedTxt.append(lineStr);
            }
        }
        textView.setText(mFoldedTxt.toString());
        expandedTextView.setVisibility(View.VISIBLE);
        expandedTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isExpanded) {
                    isExpanded = false;
                    textView.setMaxLines(3);
                    textView.setText(mFoldedTxt);
                    expandedTextView.setText("展开");
                    expandedTextView.setVisibility(View.VISIBLE);
                } else {
                    isExpanded = true;
                    textView.setMaxLines(Integer.MAX_VALUE);
                    textView.setText(mExpandedTxt);
                    expandedTextView.setText("收起");
//                        expandedTextView.setVisibility(View.GONE);
                }
            }
        });
    } else {
        textView.setText(mExpandedTxt.toString());
        expandedTextView.setVisibility(View.GONE);
    }
}

    public void showFoldedTextPick(TextView textView, TextView expandedTextView) {
        if (isRun) {
            return;
        }
        isRun = true;
        int lines = textView.getLineCount();
        Layout layout = textView.getLayout();
        mExpandedTxt = new StringBuilder(textView.getText().toString());
        if (lines > mMaxLine) {
            mFoldedTxt = new StringBuilder();
            for (int i = 0; i < mMaxLine; i++) {
                String lineStr = mExpandedTxt.subSequence(layout.getLineStart(i), layout.getLineEnd(i)).toString();
                if (i == mMaxLine - 1) {
                    try{
                        String substring = lineStr.substring(0, lineStr.length()-3) + "...";
                        mFoldedTxt.append(substring);
                    }catch (Exception e){
                        mFoldedTxt.append(lineStr);
                    }
                } else {
                    mFoldedTxt.append(lineStr);
                }
            }

            int LineCount = mExpandedTxt.subSequence(layout.getLineStart(0), layout.getLineEnd(0)).toString().length();
            textView.setText(mFoldedTxt.toString());
            expandedTextView.setVisibility(View.VISIBLE);
            expandedTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (isExpanded) {
                        isExpanded = false;
                        textView.setMaxLines(3);
                        textView.setText(mFoldedTxt);
                        expandedTextView.setText("展开");
                        expandedTextView.setVisibility(View.VISIBLE);
                    } else {
                        isExpanded = true;
                        textView.setMaxLines(Integer.MAX_VALUE);
                        if(mExpandedTxt.length() / lines == LineCount){
                            textView.setText(mExpandedTxt+"\n");
                        }else{
                            textView.setText(mExpandedTxt);
                        }
                        expandedTextView.setText("收起");
                    }
                }
            });
        } else {
            textView.setText(mExpandedTxt.toString());
            expandedTextView.setVisibility(View.GONE);
        }
    }
}
